#ifndef _KZLIB_ADS8686s_
#define _KZLIB_ADS8686s_

#include "../common_typedef.h"

enum ADC_CHIPSTA {
    ADC_CHIP_CS_COMFAIL,
    ADC_CHIP_WRONG_ID,
    ADC_CHIP_RDY,
};

enum ADC_CHANNLE {
    ADC_CHANNLE_1,
    ADC_CHANNLE_2,
    ADC_CHANNLE_3,
    ADC_CHANNLE_4,
    ADC_CHANNLE_5,
    ADC_CHANNLE_6,
    ADC_CHANNLE_7,
    ADC_CHANNLE_8,
    ADC_CHANNLE_9,
    ADC_CHANNLE_10,
    ADC_CHANNLE_11,
    ADC_CHANNLE_12,
    ADC_CHANNLE_13,
    ADC_CHANNLE_14,
    ADC_CHANNLE_15,
    ADC_CHANNLE_16,
};

constexpr uint8_t ADC8686S_ID_VALID = 0x8b;
constexpr uint8_t ADC8686S_TIMEOUT_LIMIT = 10; // ms
constexpr uint8_t ADC8686S_CHNL_MAX = 16;

struct Ads8686GpioPack {
    uint32_t cs = 0xffffffff;
    uint32_t start = 0xffffffff;
    uint32_t rdy = 0xffffffff;
};

class DrvAds8686s {
public:
    DrvAds8686s(
        const char* designator, uint32_t dataBus, Ads8686GpioPack io, void (*delayus)(uint32_t)) :
            m_dataBus(dataBus), m_io(io), m_delayus(delayus), m_designator(designator)
    {

    }
    void Init();
    void ReadState();
    uint8_t ReadChnl(uint8_t ch, int32_t& adcVal);
    const char* ChipName() { return m_designator; }

private:
    uint32_t m_dataBus = 0xffffffff;
    Ads8686GpioPack m_io;
    void    (*m_delayus)  (uint32_t);
    uint32_t m_sta = ADC_CHIP_CS_COMFAIL;
    uint32_t adcVal[ADC8686S_CHNL_MAX] = {0};
    const char* m_designator = "ADC?";
};

#endif